package net.microfigure.service.impl;

import net.microfigure.model.FamilyInfo;
import net.microfigure.model.PartInst;

import net.microfigure.commons.result.PageInfo;
import net.microfigure.mapper.PartInstMapper;
import net.microfigure.service.IFamilyInfoService;
import net.microfigure.service.IPartInstService;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 参合登记实例表 服务实现类
 * </p>
 *
 * @author 林景锋
 * @since 2018-06-22
 */
@Service
public class PartInstServiceImpl extends ServiceImpl<PartInstMapper, PartInst> implements IPartInstService {

	@Autowired
	private PartInstMapper partInstMapper;
	private PartInst partInst ;
	@Autowired
	private IFamilyInfoService familyInfoService;
	
	@Override
	public boolean insertOK(String partNo, String familyNo, String name, String gender, Integer age, String contact,
			String address) {
		// TODO Auto-generated method stub
		System.out.println("insertOK++partNo=="+partNo);
		
		partInst.setPartNo(partNo);
		partInst.setFamilyNo(familyNo);
		partInst.setName(name);
		partInst.setGender(gender);
		partInst.setAge(age);
		partInst.setContact(contact);
		partInst.setAddress(address);
		System.out.println(partInst.getAddress());
		
		
		return partInstMapper.insertOK(partInst);
	}

	/**
	 * 获取流水号
	 * 
	 * @param preFix
	 * @return
	 */

	
	/**
	 * 生成证件号
	 * 
	 * @param preFix
	 * @return
	 */
	@Override
	public String getPartInstNo(String preFix) {
//		Map<String, String> param = new HashMap<>();
//		param.put("family_no", preFix);
		
		return partInstMapper.sQLQuery(preFix);
	}

	@Override
	public void selectDataGrid(PageInfo pageInfo) {
		Page<PartInst> page = new Page<PartInst>(pageInfo.getNowpage(), pageInfo.getSize());
		EntityWrapper<PartInst> wrapper = new EntityWrapper<PartInst>();
		wrapper.orderBy(pageInfo.getSort(), pageInfo.getOrder().equalsIgnoreCase("ASC"));
		selectPage(page,wrapper);

		pageInfo.setRows(page.getRecords());
		pageInfo.setTotal(page.getTotal());

		
	}

	@Override
	public List<PartInst> selectListByFamilyInfoNo(Integer familyInfoNo) {
		// TODO Auto-generated method stub
		return partInstMapper.selectListByFamilyInfoNo(familyInfoNo);
	}

	/**
	 * 取消参合
	 * @param id
	 */
	@Override
	public void cancelPart(String partNo) {
		PartInst partInst = this.selectBypartNo(partNo);
		System.out.println("cancelPart"+partInst.getPartNo());
		//FamilyInfo familyInfo = familyInfoService.selectById(partInst.getFamilyInfoNo());
		FamilyInfo familyInfo = familyInfoService.selectByFamilyInfoNo(partInst.getFamilyInfoNo());
		familyInfo.setPartState("未参合");
		familyInfoService.updateById(familyInfo);
		this.deleteByPartNo(partNo);
	}

	private void deleteByPartNo(String partNo) {
		partInstMapper.deleteByPartNo(partNo);
		
	}

	private PartInst selectBypartNo(String partNo) {
		// TODO Auto-generated method stub
		return partInstMapper.selectBypartNo(partNo);
	}

	/**
	 * 参合登记
	 * @param partInst
	 */
	@Override
	public void addPartInst(PartInst partInst) {
//		System.out.println("到这里有"+);
		FamilyInfo familyInfo = familyInfoService.selectByFamilyInfoNo(partInst.getFamilyInfoNo());
		System.out.println("到这里有"+familyInfo);
		familyInfo.setPartState("已参合");
		familyInfoService.updateById(familyInfo);
		this.insert(partInst);
	}

	@Override
	public void selectDataGrid(PageInfo pageInfo, String familyNo) {
		
		Page<PartInst> page = new Page<PartInst>(pageInfo.getNowpage(), pageInfo.getSize());
		EntityWrapper<PartInst> wrapper = new EntityWrapper<PartInst>();
//		String name = "admin";
//		String areaNo = familyInfoMapper.selectListcodeByname(name);
//		System.out.println("dddddddddddddddddddddddddddddddddddd");
//		System.out.println("housemaster=="+housemaster);
////		String family_no =familyInfoMapper.selectIdByFamilyHousemaster(housemaster);
////		System.out.println("输出当前"+family_no);
//		String family_no =familyMapper.selectFamilyNoByHousemaster(housemaster);
//		System.out.println("dddddddddddddddddddddddddddddddddddd");
		
		System.out.println("输出当前"+familyNo);
		wrapper.where("family_no = {0}", familyNo);
//		wrapper.where("area_no = {0}",areaNo);
		wrapper.orderBy(pageInfo.getSort(),pageInfo.getOrder().equalsIgnoreCase("ASC"));
		selectPage(page,wrapper);
		pageInfo.setRows(page.getRecords());
		pageInfo.setTotal(page.getTotal());
	}




	
}
